// pages/pay/pay.js
let {
  getGoodsdetailSearch
} = require("../../api/goodsdetail");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalData: 0,
    goodsList: [],
    name: "",
    phome: "",
    adr: ""
  },
  //计算总价格方法
  getTotal() {
    //获取数据
    let goodsList = this.data.goodsList;
    console.log(goodsList);
    if (!goodsList) {
      return;
    }
    //总价格
    let data = goodsList.reduce((total, item) => {
      console.log(item.num);
      return total + item.num * item.goodsPrice;
    }, 0)
    this.setData({
      totalData: data
    })
    console.log(data);
  },

  //跳转订单页面
  goOrder() {
    wx.navigateTo({
      url: '/pages/order/order',
    })
    //生成订单数据
    this.setOrderLists(2); //支付完成

  },
  //生成订单数据
  setOrderLists(type) {
    //没有购物车数据，不执行添加订单操作
    console.log(type);
    let goodsCarlists = wx.getStorageSync('goodsCarList');
    console.log(goodsCarlists);
    if (!goodsCarlists) {
      return;
    }

    //构建数据结构
    let orderData = {
      orderId: new Date().getTime(),
      orderLists: null,
      type: type, // 1.待支付 2.代发货 3.待收货 4.待评价
      total: this.data.totalData
    }
    //添加商品
    orderData.orderLists = this.data.goodsList;
    console.log(orderData.orderLists);
    //1.判断缓存有没有数据
    let goodsCarListData = wx.getStorageSync('goodsOrderList');
    if (goodsCarListData) {
      //2.有数据添加数据
      goodsCarListData.push(orderData);
      wx.setStorageSync('goodsOrderList', goodsCarListData);
    } else {
      //3.没有添加订单
      wx.setStorageSync('goodsOrderList', [orderData])
    }

    //删除购物车数据
    wx.removeStorageSync('goodsCarList')

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let lists = []
    let goodsCarlists = wx.getStorageSync('goodsCarList');
    console.log(goodsCarlists);

    if(goodsCarlists){
      goodsCarlists.forEach(item=>{
        console.log(item);
        lists.push(item)
          //计算总价格
          this.getTotal();
          
      })
      this.setData({
        goodsList: lists
      })
    }
 

    //获取数据
    var getCarData = wx.getStorageSync('goodsCarList');
    this.setData({
      goodsList: getCarData
    })
    //计算总价格
    this.getTotal();

    let addlist = wx.getStorageSync('addlist');
    console.log(addlist);
    if(addlist != []){
      addlist.forEach(item => {
        if (item.status == 1) {
          // console.log(item.name);
          this.setData({
            name: item.name,
            phome: item.phome,
            adr: item.adr,
          })
        }
      })
    }else{
      wx.showToast({
        title: '请添加加地址',
        duration:3000,
        mask:true
      })
    }
     
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //生成订单数据
    this.setOrderLists(1); //待支付
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})